package com.example.diethealthsystem.controller;

import com.example.diethealthsystem.entity.ExerciseRecord;
import com.example.diethealthsystem.service.ExerciseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/exerciserecords")
public class ExerciseRecordController {

    private final ExerciseRecordService exerciseRecordService;

    @Autowired
    public ExerciseRecordController(ExerciseRecordService exerciseRecordService) {
        this.exerciseRecordService = exerciseRecordService;
    }

    @GetMapping
    public ResponseEntity<List<ExerciseRecord>> getAllExerciseRecords() {
        List<ExerciseRecord> exerciseRecords = exerciseRecordService.findAll();
        return new ResponseEntity<>(exerciseRecords, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<ExerciseRecord> getExerciseRecordById(@PathVariable Long id) {
        return exerciseRecordService.findById(id)
                .map(exerciseRecord -> new ResponseEntity<>(exerciseRecord, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping
    public ResponseEntity<ExerciseRecord> createExerciseRecord(@RequestBody ExerciseRecord exerciseRecord) {
        ExerciseRecord createdExerciseRecord = exerciseRecordService.save(exerciseRecord);
        return new ResponseEntity<>(createdExerciseRecord, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<ExerciseRecord> updateExerciseRecord(@PathVariable Long id, @RequestBody ExerciseRecord exerciseRecord) {
        return exerciseRecordService.findById(id)
                .map(existingExerciseRecord -> {
                    existingExerciseRecord.setUser(exerciseRecord.getUser());
                    existingExerciseRecord.setExerciseType(exerciseRecord.getExerciseType());
                    existingExerciseRecord.setDuration(exerciseRecord.getDuration());
                    existingExerciseRecord.setCaloriesBurned(exerciseRecord.getCaloriesBurned());
                    existingExerciseRecord.setRecordDate(exerciseRecord.getRecordDate());
                    ExerciseRecord updatedExerciseRecord = exerciseRecordService.save(existingExerciseRecord);
                    return new ResponseEntity<>(updatedExerciseRecord, HttpStatus.OK);
                })
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteExerciseRecord(@PathVariable Long id) {
        if (exerciseRecordService.findById(id).isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        exerciseRecordService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<ExerciseRecord>> getExerciseRecordsByUserId(@PathVariable Long userId) {
        List<ExerciseRecord> exerciseRecords = exerciseRecordService.findByUserId(userId);
        if (exerciseRecords.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(exerciseRecords, HttpStatus.OK);
    }
} 